Add a C convenience function for VFL constraints
authorEmmanuele Bassi <ebassi@gnome.org>
Sat, 29 Jun 2019 18:04:29 +0000 (19:04 +0100)
committerEmmanuele Bassi <ebassi@gnome.org>
Sun, 30 Jun 2019 22:42:45 +0000 (23:42 +0100)
The dictionary-based function is convenient for language bindings, but C
developers will feel more at home with a variadic arguments list.

demos/gtk-demo/constraints3.c
gtk/gtkconstraintlayout.c
gtk/gtkconstraintlayout.h

index fee7618f4489d525f669fc5b32e4b3a1edfd4093..648f38125bac065360131c1d0d8641249d9353d7 100644 (file)
@@ -85,25 +85,20 @@ build_constraints (VflGrid          *self,
     "V:|-[button1]-12-[button3(==button1)]-|",
     "V:|-[button2]-12-[button3(==button2)]-|",
   };
-  GHashTable *views;
   GError *error = NULL;
 
-  views = g_hash_table_new (g_str_hash, g_str_equal);
-  g_hash_table_insert (views, "button1", self->button1);
-  g_hash_table_insert (views, "button2", self->button2);
-  g_hash_table_insert (views, "button3", self->button3);
-
   gtk_constraint_layout_add_constraints_from_description (manager, vfl, G_N_ELEMENTS (vfl),
                                                           8, 8,
-                                                          views,
-                                                          &error);
+                                                          &error,
+                                                          "button1", self->button1,
+                                                          "button2", self->button2,
+                                                          "button3", self->button3,
+                                                          NULL);
   if (error != NULL)
     {
       g_printerr ("VFL parsing error:\n%s", error->message);
       g_error_free (error);
     }
-
-  g_hash_table_unref (views);
 }
 
 static void
index 25f5f8fcd816e4792dc45651121b2515aa5add5a..44554f33f442a999bebefecff1aec20ff9142dc9 100644 (file)
@@ -1612,7 +1612,7 @@ attribute_from_name (const char *name)
 }
 
 /**
- * gtk_constraint_layout_add_constraints_from_description:
+ * gtk_constraint_layout_add_constraints_from_descriptionv: (rename-to gtk_constraint_layout_add_constraints_from_description)
  * @layout: a #GtkConstraintLayout
  * @lines: (array length=n_lines): an array of Visual Format Language lines
  *   defining a set of constraints
@@ -1622,14 +1622,15 @@ attribute_from_name (const char *name)
  * @views: (element-type utf8 Gtk.Widget): a dictionary of [ name, widget ]
  *   pairs; the `name` keys map to the view names in the VFL lines, while
  *   the `widget` values map to children of the widget using a #GtkConstraintLayout
+ * @error: return location for a #GError
  *
  * Creates a list of constraints they formal description using a compact
  * description syntax called VFL, or "Visual Format Language".
  *
  * The Visual Format Language is based on Apple's AutoLayout [VFL](https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/VisualFormatLanguage.html).
  *
- * The @views dictionary is used to match widgets to the symbolic view name
- * inside the VFL.
+ * The @views dictionary is used to match #GtkConstraintTargets to the symbolic
+ * view name inside the VFL.
  *
  * The VFL grammar is:
  *
@@ -1705,13 +1706,13 @@ attribute_from_name (const char *name)
  * Returns: %TRUE if the constraints were added to the layout
  */
 gboolean
-gtk_constraint_layout_add_constraints_from_description (GtkConstraintLayout *layout,
-                                                        const char * const   lines[],
-                                                        gsize                n_lines,
-                                                        int                  hspacing,
-                                                        int                  vspacing,
-                                                        GHashTable          *views,
-                                                        GError             **error)
+gtk_constraint_layout_add_constraints_from_descriptionv (GtkConstraintLayout *layout,
+                                                         const char * const   lines[],
+                                                         gsize                n_lines,
+                                                         int                  hspacing,
+                                                         int                  vspacing,
+                                                         GHashTable          *views,
+                                                         GError             **error)
 {
   GtkConstraintVflParser *parser;
 
@@ -1802,3 +1803,79 @@ gtk_constraint_layout_add_constraints_from_description (GtkConstraintLayout *lay
 
   return TRUE;
 }
+
+/**
+ * gtk_constraint_layout_add_constraints_from_description:
+ * @layout: a #GtkConstraintLayout
+ * @lines: (array length=n_lines): an array of Visual Format Language lines
+ *   defining a set of constraints
+ * @n_lines: the number of lines
+ * @hspacing: default horizontal spacing value, or -1 for the fallback value
+ * @vspacing: default vertical spacing value, or -1 for the fallback value
+ * @error: return location for a #GError
+ * @first_view: the name of a view in the VFL description, followed by the
+ *   #GtkConstraintTarget to which it maps
+ * @...: a %NULL-terminated list of view names and #GtkConstraintTargets
+ *
+ * Creates a list of constraints they formal description using a compact
+ * description syntax called VFL, or "Visual Format Language".
+ *
+ * This function is a convenience wrapper around
+ * gtk_constraint_layout_add_constraints_from_descriptionv(), using
+ * variadic arguments to populate the view/target map.
+ *
+ * Returns: %TRUE if the constraints were added to the layout
+ */
+gboolean
+gtk_constraint_layout_add_constraints_from_description (GtkConstraintLayout *layout,
+                                                        const char * const   lines[],
+                                                        gsize                n_lines,
+                                                        int                  hspacing,
+                                                        int                  vspacing,
+                                                        GError             **error,
+                                                        const char          *first_view,
+                                                        ...)
+{
+  GtkConstraintVflParser *parser;
+  GHashTable *views;
+  const char *view;
+  gboolean res;
+  va_list args;
+
+  g_return_val_if_fail (GTK_IS_CONSTRAINT_LAYOUT (layout), FALSE);
+  g_return_val_if_fail (lines != NULL, FALSE);
+  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+  g_return_val_if_fail (first_view != NULL, FALSE);
+
+  parser = gtk_constraint_vfl_parser_new ();
+  gtk_constraint_vfl_parser_set_default_spacing (parser, hspacing, vspacing);
+
+  views = g_hash_table_new (g_str_hash, g_str_equal);
+
+  va_start (args, first_view);
+
+  view = first_view;
+  while (view != NULL)
+    {
+      GtkConstraintTarget *target = va_arg (args, GtkConstraintTarget *);
+
+      if (target == NULL)
+        break;
+
+      g_hash_table_insert (views, (gpointer) view, target);
+
+      view = va_arg (args, const char *);
+    }
+
+  va_end (args);
+
+  res =
+    gtk_constraint_layout_add_constraints_from_descriptionv (layout, lines, n_lines,
+                                                             hspacing, vspacing,
+                                                             views,
+                                                             error);
+
+  g_hash_table_unref (views);
+
+  return res;
+}
index 39d3aecaf0ca9d1dcbff5c845c917a935ec834b3..227a8bfbc75d177917adf946266c74312979fa54 100644 (file)
@@ -78,6 +78,15 @@ void                    gtk_constraint_layout_remove_guide      (GtkConstraintLa
 
 GDK_AVAILABLE_IN_ALL
 gboolean                gtk_constraint_layout_add_constraints_from_description  (GtkConstraintLayout *manager,
+                                                                                 const char * const   lines[],
+                                                                                 gsize                n_lines,
+                                                                                 int                  hspacing,
+                                                                                 int                  vspacing,
+                                                                                 GError             **error,
+                                                                                 const char          *first_view,
+                                                                                 ...) G_GNUC_NULL_TERMINATED;
+GDK_AVAILABLE_IN_ALL
+gboolean                gtk_constraint_layout_add_constraints_from_descriptionv (GtkConstraintLayout *manager,
                                                                                  const char * const   lines[],
                                                                                  gsize                n_lines,
                                                                                  int                  hspacing,